<?php
/**
 * Copyright (c) [2019] [吴跃忠]
 * [selibra] is licensed under the Mulan PSL v1.
 * You can use this software according to the terms and conditions of the Mulan PSL v1.
 * You may obtain a copy of Mulan PSL v1 at:
 * http://license.coscl.org.cn/MulanPSL
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v1 for more details.
 */

namespace Selibra\Http\Route;


class RouterConfig
{

    const REQUEST_POST = 'POST';// POST请求
    const REQUEST_GET = 'GET';// GET请求
    const REQUEST_DELETE = 'DELETE';// DELETE请求
    const REQUEST_PUT = 'PUT';// PUT请求
    const REQUEST_HEAD = 'HEAD';// HEAD请求
    const REQUEST_OPTIONS = 'OPTIONS';// OPTIONS请求
    const REQUEST_TRACE = 'TRACE';// TRACE请求
    const REQUEST_CONNECT = 'CONNECT';// CONNECT请求


    /**
     * 请求类型
     * @var mixed
     */
    protected $type = 'ANY';


    /**
     * 路由别名
     * @var string
     */
    protected string $alias;


    /**
     * @var string
     */
    protected string $class;


    /**
     * @var string
     */
    protected string $method;


    /**
     * 是否熔断
     * 如果该值为TRUE，再高并发访问时，将会对该路由进行访问限制
     * @var bool
     */
    protected bool $accessFuses = false;

    /**
     * @var float
     */
    protected float $version = 0.1;

    /**
     * 预定义参数
     * /g/{param0}/{param1}
     * 其中param0和param1为预定义参数
     * 预定义参数主要用户将动态参数放置到路由地址中
     * @var array
     */
    protected array $params = [];

    /**
     * @return float
     */
    public function getVersion(): float
    {
        return $this->version;
    }

    /**
     * @param float $version
     * @return RouterConfig
     */
    public function setVersion(float $version): RouterConfig
    {
        $this->version = $version;
        return $this;
    }


    public function __construct($config = [])
    {
        $this->setConfig($config);
    }


    /**
     * @param $config
     */
    public function setConfig($config)
    {
        $vars = get_class_vars(self::class);
        foreach ($vars as $var => $varVal) {
            if (isset($config[$var])) {
                $this->$var = $config[$var];
            }
        }
    }


    /**
     * 获取请求类型
     * @return mixed
     */
    public function getType()
    {
        return $this->type;
    }


    /**
     * 设置类型
     * @param mixed $type
     */
    public function setType($type): void
    {
        $this->type = strtoupper($type);
    }


    /**
     * @return string
     */
    public function getAlias(): string
    {
        return $this->alias;
    }


    /**
     * @param string $alias
     */
    public function setAlias(string $alias): void
    {
        $this->alias = $alias;
    }


    /**
     * @return bool
     */
    public function isAccessFuses(): bool
    {
        return $this->accessFuses;
    }


    /**
     * @param bool $accessFuses
     */
    public function setAccessFuses(bool $accessFuses): void
    {
        $this->accessFuses = $accessFuses;
    }

    /**
     * @return string
     */
    public function getClass(): string
    {
        return $this->class;
    }

    /**
     * @param string $class
     */
    public function setClass(string $class): void
    {
        $this->class = $class;
    }

    /**
     * @return string
     */
    public function getMethod(): string
    {
        return $this->method;
    }

    /**
     * @param string $method
     */
    public function setMethod(string $method): void
    {
        $this->method = $method;
    }

    /**
     * @return array
     */
    public function getParams(): array
    {
        return $this->params;
    }

    /**
     * @param array $params
     * @return RouterConfig
     */
    public function setParams(array $params): RouterConfig
    {
        $this->params = $params;
        return $this;
    }

}
